﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Printing;
using System.Drawing;

namespace sands
{
    public class PrintDaysDeposits : SandsPrintDocument
    {
        protected List<String[]> listOfDeposits;
        protected String reportNumber = String.Empty;

        public PrintDaysDeposits(String number,String date,List<String[]> input)
            : base(date)
        {
            reportNumber = number;
            columnsNames = new String[] { "L.p.", "Imię i nazwisko", "Numer KP / przelewu", "Tytuł wpłaty", "Kwota" };
            columns = new Rectangle[columnsNames.Length];
            listOfDeposits = input;
        }

        protected override void DrawTitle(ref int top)
        {
            //heading
            Image img = Image.FromFile("logo_sands.bmp");
            gdiPage.DrawImage(img, new Rectangle(top, 100, img.Width, img.Height));
            top += img.Height;

            gdiPage.DrawString("SMITH & SCHMIDT\nszkoła języków obcych\nKu Studzionkom 12\n34-400 Nowy Targ", Font, Brushes.Black, new Point(100, top));
            top += 4 * Font.Height; // above takes 3 lines

            //title
            top += 2 * Font.Height; //2 line space

            String tmp = "Zestawienie wpłat " + reportNumber +" dokonanych w dniu " + date;
            gdiPage.DrawString(tmp, Font, Brushes.Black, 425 - gdiPage.MeasureString(tmp, Font).Width / 2, top);

            top += 3 * Font.Height; //space
        }

        protected override void DrawHeaders(ref int top)
        {
            //consatnt used to set the width of the columns
            int constant = (int)gdiPage.MeasureString("O", Font).Width;

            //columns config
            //Lp
            columns[0] = new Rectangle(100, top, (int)gdiPage.MeasureString(columnsNames[0], Font).Width, (int)Font.Height);
            //imie i nazwisko
            columns[1] = new Rectangle(columns[0].Right, top, 13 * constant, (int)Font.Height);
            //numer
            columns[2] = new Rectangle(columns[1].Right, top, 10 * constant, (int)Font.Height);
            //tytul
            columns[3] = new Rectangle(columns[2].Right,top, 14 * constant, (int)Font.Height);
            //kwota
            columns[4] = new Rectangle(columns[3].Right,top, 7 * constant, (int)Font.Height);

            //paint and draw
            gdiPage.FillRectangles(Brushes.Wheat, columns);
            gdiPage.DrawRectangles(Pens.Black, columns);

            //print headers
            for (int i = 0; i < columnsNames.Length; i++)
                gdiPage.DrawString(columnsNames[i], Font, Brushes.Black, columns[i].Location);

            top += (int)columns[0].Height; //go to the next row
        }

        /// <summary>
        /// Draws the deposits made on a specific date
        /// </summary>
        /// <param name="top"></param>
        protected override bool DrawTable(ref int top)
        {
            String sum = "";

            while(rowNumber<listOfDeposits.Count)
            {
                if (listOfDeposits.Count < 1) //empty
                    return false;

                if (!listOfDeposits[rowNumber][0].Equals(String.Empty)) //is that the last row containing the sum?
                {
                    //first cell (alignment)
                    Rectangle cell = new Rectangle(new Point(columns[0].Left, top), columns[0].Size);
                    gdiPage.DrawString(listOfDeposits[rowNumber][0], Font, Brushes.Black, Align(listOfDeposits[rowNumber][0], cell, AlignType.Right));
                    gdiPage.DrawRectangle(Pens.Black, cell);

                    //rest
                    for (int j = 1; j < listOfDeposits[rowNumber].Length - 1; j++)
                    {
                        //new row
                        cell = new Rectangle(new Point(columns[j].Left, top), columns[j].Size);
                        gdiPage.DrawString(listOfDeposits[rowNumber][j], Font, Brushes.Black, cell.Location);
                        gdiPage.DrawRectangle(Pens.Black, cell);
                    }

                    //last cell (alignment)
                    cell = new Rectangle(new Point(columns[listOfDeposits[rowNumber].Length - 1].Left, top), columns[listOfDeposits[rowNumber].Length - 1].Size);
                    gdiPage.DrawString(listOfDeposits[rowNumber][listOfDeposits[rowNumber].Length - 1], Font, Brushes.Black, Align(listOfDeposits[rowNumber][listOfDeposits[rowNumber].Length - 1], cell, AlignType.Right));
                    gdiPage.DrawRectangle(Pens.Black, cell);

                    if (top + (int)columns[0].Height > 1000) //more than one page
                        return true;


                    top += (int)columns[0].Height; //go to next row
                }
                else
                    sum = listOfDeposits[rowNumber][listOfDeposits[rowNumber].Length - 1];

                ++rowNumber;
            }

            //last row: cell with sum
            Rectangle valueRow = new Rectangle(new Point(columns[4].Left, top), columns[4].Size);
            gdiPage.FillRectangle(Brushes.Wheat, valueRow);
            gdiPage.DrawString(sum, Font, Brushes.Black, Align(sum, valueRow, AlignType.Right));
            gdiPage.DrawRectangle(Pens.Black, valueRow);

            //last row: cell with description
            Rectangle descRow = new Rectangle(columns[0].Left, top, columns[3].Right - columns[0].Left, columns[0].Height);
            gdiPage.FillRectangle(Brushes.Wheat, descRow);
            gdiPage.DrawString("Razem:", Font, Brushes.Black, descRow.Location);
            gdiPage.DrawRectangle(Pens.Black, descRow);

            top += 5 * Font.Height; //go down

            gdiPage.DrawString("....................................", Font, Brushes.Black, 100, top);
            top += Font.Height;
            gdiPage.DrawString("podpis sporządzającego", Font, Brushes.Black, 100, top);

            rowNumber = 0;

            return false;
        }
    }
}
